/*
* @Description:网络扩展-选择组织
* @Author: lil
* @Date: 2021/11/22
**/
<template>
  <div class="select-channel">
    <p>选择需要加入该通道的组织</p>
    <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" border>
      <el-table-column label="选择" align="center" type="selection" width="100">
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="domainName" label="域名" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="orgaType" label="组织类型" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="hostName" label="主机" align="center" show-overflow-tooltip>
      </el-table-column>
    </el-table>
    <div align="center" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </div>
</template>
<script>
  import {
    getOldChannel,
    getChannelOrg
  } from '@/api/blockchain/networkmanage'
  export default {
    props: ['channelName', 'channelOrgs'],
    data() {
      return {
        multipleSelection: [],
        tableData: [],
        channels: [],
        initChoice: []
      };
    },
    mounted() {
      this.getOneOldChannel();
    },
    methods: {
      getOneOldChannel() {
        let _this = this;
        getOldChannel(_this.channelName, _this.channelOrgs).then((res) => {
          for (let i = 0; i < res.length; i++) {
            _this.tableData.push({
              name: '',
              domainName: '',
              orgaType: '',
              hostName: ''
            });
            _this.tableData[i].name = res[i].name;
            _this.tableData[i].domainName = res[i].domain;
            _this.tableData[i].orgaType = res[i].type;
            _this.tableData[i].hostName = res[i].hosts;
            if (_this.channelOrgs.split(',').indexOf(res[i].name) != -1) {
              _this.$refs.multipleTable.toggleRowSelection(_this.tableData[i]);
            }
          }
          _this.initChoice = JSON.parse(JSON.stringify(_this.$refs.multipleTable.selection))
        }).catch((err) => {
          console.log(err);
        });
      },
      getChannelOrgs() {
        let _this = this;
        let alisArr = [];
        this.multipleSelection.forEach(item => {
          alisArr.push(item.name);
        })
        if (!alisArr.length && !this.initChoice.length) {
          this.$message.warning('请选择要扩展的组织！')
          return
        }
        getChannelOrg(alisArr.join(',')).then((res) => {
          _this.$emit('close-dialog', true);
        }).catch((err) => {
          console.log(err);
        });
      },
      handleSelectionChange(vals) {
        this.multipleSelection = vals;
      },
      confirm() {
        this.getChannelOrgs();
      },
    }
  };

</script>
<style lang="scss" scoped>
  .select-channel {
    padding: 20px 30px;
  }

  .dialog-footer {
    margin-top: 40px;
  }

  .el-table>>>.el-table__row .el-table-column--selection .cell {
    padding: 0;
  }

</style>
